using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class TenantDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public TenantDA()
		{
		}

		public TenantDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Tenant table.
		/// </summary>
		public  int Insert(Tenant tenant)
		{
			ValidationUtility.ValidateArgument("tenant", tenant);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantTypeID", tenant.TenantTypeID),
				new SqlParameter("@TenantDescription", tenant.TenantDescription),
				new SqlParameter("@TenantImage", tenant.TenantImage),
				new SqlParameter("@IsActive", tenant.IsActive),
				new SqlParameter("@TenantSecureCode", tenant.TenantSecureCode),
				new SqlParameter("@CreatedDate", tenant.CreatedDate),
				new SqlParameter("@SubscriptionEndDate", tenant.SubscriptionEndDate),
				new SqlParameter("@UpdateDate", tenant.UpdateDate),
				new SqlParameter("@UpdatedBy", tenant.UpdatedBy)
			};

			tenant.TenantID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "TenantInsert", parameters));
			return tenant.TenantID;
		}

		/// <summary>
		/// Updates a record in the Tenant table.
		/// </summary>
		public  int Update(Tenant tenant)
		{
			ValidationUtility.ValidateArgument("tenant", tenant);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenant.TenantID),
				new SqlParameter("@TenantTypeID", tenant.TenantTypeID),
				new SqlParameter("@TenantDescription", tenant.TenantDescription),
				new SqlParameter("@TenantImage", tenant.TenantImage),
				new SqlParameter("@IsActive", tenant.IsActive),
				new SqlParameter("@TenantSecureCode", tenant.TenantSecureCode),
				new SqlParameter("@CreatedDate", tenant.CreatedDate),
				new SqlParameter("@SubscriptionEndDate", tenant.SubscriptionEndDate),
				new SqlParameter("@UpdateDate", tenant.UpdateDate),
				new SqlParameter("@UpdatedBy", tenant.UpdatedBy)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "TenantUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the Tenant table by its primary key.
		/// </summary>
		public  int Delete(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "TenantDelete", parameters));
		}

		/// <summary>
		/// Selects a single record from the Tenant table.
		/// </summary>
		public  Tenant Select(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "TenantSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeTenant(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Tenant table.
		/// </summary>
		public  List<Tenant> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "TenantSelectAll", parameters))
			{
				List<Tenant> tenantList = new List<Tenant>();
				while (dataReader.Read())
				{
					Tenant tenant = MakeTenant(dataReader);
					tenantList.Add(tenant);
				}

				return tenantList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Tenant class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  Tenant MakeTenant(SqlDataReader dataReader)
		{
			Tenant tenant = new Tenant();
			tenant.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			tenant.TenantTypeID = SqlClientUtility.GetString(dataReader, "TenantTypeID", String.Empty);
			tenant.TenantDescription = SqlClientUtility.GetString(dataReader, "TenantDescription", String.Empty);
			tenant.TenantImage = SqlClientUtility.GetString(dataReader, "TenantImage", String.Empty);
			tenant.IsActive = SqlClientUtility.GetBoolean(dataReader, "IsActive", false);
			tenant.TenantSecureCode = SqlClientUtility.GetString(dataReader, "TenantSecureCode", String.Empty);
			tenant.CreatedDate = SqlClientUtility.GetDateTime(dataReader, "CreatedDate", DateTime.Now);
			tenant.SubscriptionEndDate = SqlClientUtility.GetDateTime(dataReader, "SubscriptionEndDate", DateTime.Now);
			tenant.UpdateDate = SqlClientUtility.GetDateTime(dataReader, "UpdateDate", DateTime.Now);
			tenant.UpdatedBy = SqlClientUtility.GetString(dataReader, "UpdatedBy", String.Empty);

			return tenant;
		}

		#endregion
	}
}
